# -*- coding:utf-8 -*-

from rest_framework import serializers
from manufacturer.models import AuthorityCode, StatusMsg, AlarmMsg, ElevatorMalfunction, ElevatorMalfunctionDeal, \
    RepairContent, RepairType, ElevatorStatus, ElevatorFault, CompanyPlatform, Manufacturer, ElevatorWarning, \
    ElevatorFaultAnalysis, AuthorityCodeUserRelation
import datetime
from basic.models import ElevatorInfo


class AuthorityCodeSerializer(serializers.ModelSerializer):
    class Meta:
        model = AuthorityCode
        fields = ("manufacturer_code_elevator", "elevator_code", "manufacturer_number_elevator", "device_code_elevator",
                  "device_type", "sim_no", "collection_num", "address", "maintain_company", "maintain_company_name",
                  "property_company", "property_company_name", "elevator_type", "jump_layer", "half_layer", "layer",
                  "start_floor")


class AuthorityCodeListSerializer(serializers.ModelSerializer):
    class Meta:
        model = AuthorityCode
        fields = ("id", "manufacturer_code_elevator", "elevator_code", "manufacturer_number_elevator", "device_code_elevator",
                  "device_type", "sim_no", "collection_num", "address", "maintain_company_name", "maintain_company",
                  "property_company_name", "elevator_type", "manufacturer_name", "property_company", "jump_layer",
                  "half_layer", "layer", "start_floor", "register_log")


class StatusMsgSerializer(serializers.ModelSerializer):
    """二期功能电梯状态序列化"""
    property_company_name = serializers.SerializerMethodField()
    internal_no = serializers.SerializerMethodField()
    status = serializers.SerializerMethodField()
    alarm = serializers.SerializerMethodField()

    class Meta:
        model = StatusMsg
        fields = ("property_company_name", "internal_no", "status", "alarm")

    def get_alarm(self, obj):
        now = datetime.datetime.now()
        today = datetime.datetime(year=now.year, month=now.month, day=now.day)
        alarm = AlarmMsg.objects.filter_active(authority_code=obj.authority_code, create_time__gte=today)\
            .order_by("-id").first()
        # TODO
        # n = random.choice((0, 1))
        alarm = AlarmMsg.objects.filter_active(id=1).first()
        return alarm.get_elevator_alarm()
        # return alarm.get_elevator_alarm() if alarm else []

    def get_property_company_name(self, obj):
        register_code = obj.authority_code.elevator_code
        if not register_code:
            return u"中南海"
        elevator = ElevatorInfo.objects.filter_active(register_code=register_code).first()
        return elevator.property_company.name

    def get_internal_no(self, obj):
        register_code = obj.authority_code.elevator_code
        if not register_code:
            return u"中南海1号电梯"
        elevator = ElevatorInfo.objects.filter_active(register_code=register_code).first()
        return elevator.internal_no

    def get_status(self, obj):
        return obj.get_elevator_status()


class ElevatorFaultSerializer(serializers.ModelSerializer):
    """二期功能电梯故障序列化"""

    class Meta:
        model = ElevatorFault
        fields = ("id", "create_time", "alarm_position", "alarm_type", "notify_person", "receipt_status",
                  "notify_method", "register_code", "manufacturer_number", "elevator", "is_online", "sn", "o", "address",
                  "status_id")


class ElevatorMalfunctionSerializer(serializers.ModelSerializer):
    """ web post"""

    class Meta:
        model = ElevatorMalfunction
        fields = ("id", "maintenance_type", "register_code", "elevator", "pro_no", "factory_code", "per_unique_code",
                  "alarm_time", "equ_loc_addr", "alarm_types", "notify_person", "start_planning_time",
                  "end_planning_time", "service_id", "property_company", "maintain_company", "repair_content",
                  "reason", "work_hours", "costs", "time_points", "status", "property_company_name",
                  "maintain_company_name", "pic")


class ElevatorMalfunctionListSerializer(serializers.ModelSerializer):
    """ web list"""

    class Meta:
        model = ElevatorMalfunction
        fields = ("id", "maintenance_type", "register_code", "elevator", "pro_no", "factory_code", "per_unique_code",
                  "alarm_time", "equ_loc_addr", "alarm_types", "notify_person", "start_planning_time",
                  "end_planning_time", "status", "property_company_name", "maintain_company_name",
                  "repair_content", "pictures", "create_by_user")


class ElevatorMalfunctionDetailSerializer(serializers.ModelSerializer):
    """ web get"""

    class Meta:
        model = ElevatorMalfunction
        fields = ("id", "maintenance_type", "register_code", "elevator", "pro_no", "factory_code", "per_unique_code",
                  "alarm_time", "equ_loc_addr", "alarm_types", "notify_person", "start_planning_time",
                  "end_planning_time", "status", "property_company_name", "maintain_company_name", "pictures",
                  "repair_content", "pictures", "notify_person_ids", "costs", "work_hours", "pictures_success",
                  "signature", "signature_success")


class RepairContentSerializer(serializers.ModelSerializer):
    """ web MaintenanceContent """

    can_be_modified = serializers.SerializerMethodField()

    class Meta:
        model = RepairContent
        fields = ("id", "repair_type_name", "repair_type", "content",
                  "can_be_modified", "comment", "costs", "name")

    def get_can_be_modified(self, obj):
        request = self.context.get("request", None)
        user = request.user
        return obj.service_id == user.service_id


class RepairTypeSerializer(serializers.ModelSerializer):
    """ web RepairType """

    can_be_modified = serializers.SerializerMethodField()

    class Meta:
        model = RepairType
        fields = ("id", "name", "can_be_modified")

    def get_can_be_modified(self, obj):
        request = self.context.get("request", None)
        user = request.user
        return obj.service_id == user.service_id


class AppElevatorMalfunctionListSerializer(serializers.ModelSerializer):
    """ app list """

    class Meta:
        model = ElevatorMalfunction
        fields = ("id", "maintenance_type", "register_code", "elevator", "pro_no", "factory_code", "per_unique_code",
                  "alarm_time", "equ_loc_addr", "alarm_types", "notify_person", "start_planning_time",
                  "end_planning_time", "status", "property_company_name", "maintain_company_name", "repair_content",
                  "desc", "solution", "actual_costs", "time_points")


class AppElevatorMalfunctionDetailSerializer(serializers.ModelSerializer):
    """ app get """

    class Meta:
        model = ElevatorMalfunction
        fields = ("id", "maintenance_type", "register_code", "elevator", "pro_no", "factory_code", "per_unique_code",
                  "alarm_time", "equ_loc_addr", "alarm_types", "notify_person", "start_planning_time",
                  "end_planning_time", "status", "property_company_name", "maintain_company_name", "repair_content",
                  "desc", "solution", "qr_code", "reason", "work_hours", "costs", "pic", "actual_costs", "signature",
                  "time_points_dic", "pic_success", "work_hours_success")


class AppElevatorMalfunctionSerializer(serializers.ModelSerializer):
    """ app update create """

    class Meta:
        model = ElevatorMalfunction
        fields = ("id", "maintenance_type", "register_code", "elevator", "pro_no", "factory_code", "per_unique_code",
                  "alarm_time", "equ_loc_addr", "alarm_types", "notify_person", "start_planning_time",
                  "end_planning_time", "status", "property_company", "maintain_company", "repair_content", "time_points",
                  "desc", "solution", "qr_code", "reason", "work_hours", "costs", "create_user", "service_id", "pic",
                  "solution", "pic_success", "maintain_company_name", "property_company_name", "pic_success",
                  "work_hours_success", "actual_costs", "signature")


class StatusMonitorListSerializer(serializers.ModelSerializer):

    e = serializers.SerializerMethodField()
    product_company_name = serializers.SerializerMethodField()
    follow = serializers.SerializerMethodField()
    # id = serializers.SerializerMethodField()

    class Meta:
        model = AuthorityCode
        fields = ("id", "e", "manufacturer_number_elevator", "address", "property_company_name", "product_company_name",
                  "maintain_company_name", "is_online", "company_platform", "update_time", "o", "f", "create_time",
                  "button_display", "follow", "surl")

    # def get_id(self, obj):
    #     o = obj.device_code_elevator
    #     es = ElevatorStatus.objects.filter_active(o=o).first()
    #     return es.id if es else None

    def get_e(self, obj):
        return obj. elevator_code

    def get_product_company_name(self, obj):
        return obj.manufacturer_name()

    def get_follow(self, obj):
        user = self.context['request'].user
        return AuthorityCodeUserRelation.objects.filter_active(authority_code=obj, user=user).count()


class StatusMonitorAppListSerializer(serializers.ModelSerializer):

    e = serializers.SerializerMethodField()
    register_code = serializers.SerializerMethodField()
    product_company_name = serializers.SerializerMethodField()
    # id = serializers.SerializerMethodField()

    class Meta:
        model = AuthorityCode
        fields = ("id", "e", "manufacturer_number_elevator", "address", "property_company_name", "product_company_name",
                  "maintain_company_name", "is_online", "company_platform", "update_time", "o", "f", "create_time",
                  "internal_no", "register_code")

    def get_e(self, obj):
        return obj.elevator_code

    def get_register_code(self, obj):
        return obj.elevator_code

    # def get_id(self, obj):
    #     o = obj.device_code_elevator
    #     es = ElevatorStatus.objects.filter_active(o=o).first()
    #     return es.id if es else None

    def get_product_company_name(self, obj):
        return obj.manufacturer_name()


class StatusMonitorSerializer(serializers.ModelSerializer):

    class Meta:
        model = ElevatorStatus
        fields = ("id", "property_company_name", "internal_no", "cp", "is_open", "run_status")


class ElevatorFaultListSerializer(serializers.ModelSerializer):

    notify_person = serializers.SerializerMethodField()
    receipt_status = serializers.SerializerMethodField()
    notify_method = serializers.SerializerMethodField()

    class Meta:
        model = ElevatorFault
        fields = ("id", "alarm_type", "create_time", "alarm_position", "notify_person", "receipt_status", "notify_method")

    def get_notify_person(self, obj):
        return None

    def get_receipt_status(self, obj):
        return None

    def get_notify_method(self, obj):
        return None


class CompanyPlatformSerializer(serializers.ModelSerializer):

    class Meta:
        model = CompanyPlatform
        fields = ("id", "name", "web_name", "code", "can_access", "domain", "port", "is_online", "unique_id",
                  "manufacturers", "desc")


class ManufacturerForPlatformSerializer(serializers.ModelSerializer):

    class Meta:
        model = Manufacturer
        fields = ("id", "name", "code")


class ManufacturerSerializer(serializers.ModelSerializer):

    class Meta:
        model = Manufacturer
        fields = ("id", "name", "brand_name", "code", "address", "manu_phone", "contact", "contact_phone")


class AppElevatorWarningSerializer(serializers.ModelSerializer):

    class Meta:
        model = ElevatorFault
        fields = ("id", "f", "o", "s", "register_code", "t", "address", "property_company", "inner_no", "content", "create_time")


class ElevatorFaultAnalysisSerializer(serializers.ModelSerializer):

    class Meta:
        model = ElevatorFaultAnalysis
        fields = ("id", "materials", "name", "etype", "solution", "property_company_name", "property_company",
                  "auth_user", "username", "stime")


















